package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.dao.RolePermissionDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.RolePermission;
import com.itheima.health.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

@DubboService
@Slf4j
public class PermissionServiceImpl implements PermissionService {


    @Autowired
    PermissionDao permissionDao;
    @Autowired
    RolePermissionDao rolePermissionDao;
    /**
     * @Author: 涵哥
     * @Date: 14:57 2021/1/19
     * @Parms [queryPageBean]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 对权限的动态维护-查询
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        log.info("[权限数据-分页查询]queryPageBean: {}",queryPageBean);

        //调用分页插件分页
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());

        //分页查询
        Example example = new Example(Permission.class);
        Example.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(queryPageBean.getQueryString())){
            criteria.orLike("id","%"+queryPageBean.getQueryString()+"%");
            criteria.orLike("name","%"+queryPageBean.getQueryString()+"%");
        }

        Page<Permission> page = (Page<Permission>) permissionDao.selectByExample(example);
        return new PageResult(page.getTotal(),page.getResult());
    }


    /**
     * @Author: 涵哥
     * @Date: 16:14 2021/1/19
     * @Parms [permission]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 权限动态维护-新增
     */
    @Override
    public void add(Permission permission) {
        log.info("[权限维护-新增]data: {}",permission);
        permissionDao.insertSelective(permission);
    }

    /**
     * @Author: 涵哥
     * @Date: 16:14 2021/1/19
     * @Parms [permission]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 权限动态维护-根据id删除
     */
    @Override
    public void deleteById(Integer id) {
        log.info("[权限动态维护-根据id删除]data : {}",id);
        //先查询是否被使用
        RolePermission rolePermission = new RolePermission();
        rolePermission.setPermissionId(id);
        int i = rolePermissionDao.selectCount(rolePermission);
        if (i>0){
            throw new BusinessRuntimeException("该数据无法删除,请重新操作");
        }
        permissionDao.deleteByPrimaryKey(id);
    }


    /**
     * @Author: 涵哥
     * @Date: 16:14 2021/1/19
     * @Parms
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 权限动态维护-编辑
     */
    @Override
    public void edit(Permission permission) {
        log.info("[权限动态维护-编辑]data: {}",permission);
        permissionDao.updateByPrimaryKeySelective(permission);
    }

    /**
     * @Author: 涵哥
     * @Date: 17:55 2021/1/19
     * @Parms [id]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 编辑的数据回显-据id查询
     */
    @Override
    public Permission findById(Integer id) {
        log.info("[编辑数据回显-据id返回]id: {}",id);
        Permission permission = permissionDao.selectByPrimaryKey(id);
        return permission;
    }


}
